/*
 * Copyright (C) 2012 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.navigation.point;

import android.content.Intent;
import android.location.Location;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import ch.bfh.aeneas.exchange.Scope;
import ch.bfh.aeneas.exchange.data.TrackStorage;
import ch.bfh.aeneas.exchange.intent.AeneasIntent;
import ch.bfh.aeneas.exchange.intent.MalformedIntentException;
import ch.bfh.aeneas.navigation.app.NavigationService;
import java.util.ArrayList;
import java.util.List;

/**
 * This is the backing service of PointMe. It adds the new POI's to the storage.
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class PointService extends NavigationService {

    
    public class PointServiceBinder extends Binder {

        public PointService getService() {
            return PointService.this;
        }
    }
    private final PointServiceBinder mBinder = new PointServiceBinder();

    private final static String LOG_TAG = "PointService";
    
    private List<TrackStorage> mStorages;
    private int mStorageRequest;
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onCreate() {
        super.onCreate();
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public IBinder onBind(Intent intent) {
        super.onBind(intent);
        
        try {
            mStorages = new ArrayList<TrackStorage>();
            Intent request = getBroadcastIntent(AeneasIntent.ACTION_REQUEST, AeneasIntent.CATEGORY_TRACKSTORAGE);
            request.putExtra("scope", Scope.LOCAL);
            request.putExtra("type", "storage");
            super.broadcast(request, 10000L);
        } catch (MalformedIntentException ex) {
            Log.w(LOG_TAG, "Error while retrieving storage for node");
        }
            
        return mBinder;
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    protected void onRequestClosed(int id) {
        Log.i(LOG_TAG, "closed request: "+id);
        super.setReady();
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    protected void onAnswer(Intent i) {
        Log.i(LOG_TAG, "received answer!");
        if(i.hasExtra("storage")) {
            Log.i(LOG_TAG, "And it even has a storage!");
            TrackStorage s = (TrackStorage) i.getParcelableExtra("storage");
            mStorages.add(s);
        }
    }
    
    public void addPoi(String poiName, Location loc) {
        Log.i(LOG_TAG, "Pushing poi to "+mStorages.size()+" storages");
        for(int i = 0; i < mStorages.size(); i++) {
            mStorages.get(i).addPOI(poiName, loc);
        }
    }
    
    public void addPoi(String poiName, double longitude, double latitude, double altitude, float accuracy) {
        Log.i(LOG_TAG, "Pushing poi to "+mStorages.size()+" storages");
        for(int i = 0; i < mStorages.size(); i++) {
            mStorages.get(i).addPOI(poiName, latitude, longitude, altitude, accuracy);
        }
    }
}
